#include "playscene.h"
#include <qdebug.h>
#include <QMenuBar>
#include<QPainter>
#include "mypushbutton.h"
#include <qtimer.h>
#include <QLabel>
#include "dataconfig.h"
#include <QPropertyAnimation>
#include <qsound.h>

PlayScene::PlayScene(QWidget *parent) : QMainWindow(parent)
{

}

PlayScene::PlayScene(int levelNum):levelIndex(levelNum)
{
    qDebug()<<QString("进入了%1关").arg(levelIndex+1);

    //初始化游戏场景
    this->setFixedSize(320, 588);

    this->setWindowIcon(QPixmap(":/res/Coin0001.png"));

    this->setWindowTitle("翻金币场景");

    // 创建菜单栏
    QMenuBar *bar = menuBar();
    setMenuBar(bar);

    // 创建开始菜单
    QMenu *startMenu = bar->addMenu("开始");

    // 创建退出
    QAction *quitAction = startMenu->addAction("退出");

    // 点击退出
    connect(quitAction, &QAction::triggered, [=](){
        this->close();
    });


    QSound *backSound = new QSound(":/res/BackButtonSound.wav", this);
    QSound *flipSound = new QSound(":/res/ConFlipSound.wav", this);
    QSound *winSound = new QSound(":/res/LevelWinSound.wav", this);

    // 返回按钮
    MyPushButton * backBtn = new MyPushButton(":/res/BackButton.png", ":/res/BackButtonSelected.png");
    backBtn->setParent(this);
    backBtn->move(this->width()-backBtn->width(), this->height()-backBtn->height());

    connect(backBtn, &QPushButton::clicked, [=](){
        qDebug()<<"翻金币返回";
        backSound->play();
        QTimer::singleShot(300, this, [=](){
            emit this->chooseSceneBack();
        });
    });

    QLabel *lbl = new QLabel;
    lbl->setParent(this);
    QFont font;
    font.setFamily("华文新魏");
    font.setBold(true);
    font.setPointSize(20);
    lbl->setFont(font);
    lbl->setText(QString("Level: %1").arg(this->levelIndex+1));
    lbl->setGeometry(30, height()-50, 120,50);

    dataConfig config;
    // 初始化关卡的二维数据
    for(int i=0;i<4;++i)for(int j=0;j<4;++j){
        this->gameArray[i][j] = config.mData[levelIndex+1][i][j];
    }


    QLabel *winLabel = new QLabel;
    winLabel->setParent(this);
    QPixmap tmpPix;
    tmpPix.load(":/res/LevelCompletedDialogBg.png");
    winLabel->setGeometry(0,0, tmpPix.width(), tmpPix.height());
    winLabel->setPixmap(tmpPix);
    winLabel->move(this->width()/2-tmpPix.width()/2, -tmpPix.height());

    for(int i=0;i<4;++i)for(int j=0;j<4;++j){
        // 金币背景图案
        QPixmap pix = QPixmap(":/res/BoardNode.png");
        QLabel *lbl = new QLabel;
        lbl->setParent(this);
        lbl->setGeometry(0,0, pix.width(), pix.height());
        lbl->setPixmap(pix);
        lbl->move(57+i*50, 200+j*50);
        lbl->setAttribute(Qt::WA_TransparentForMouseEvents);

        QString str = ":/res/Coin0008.png";
        if(gameArray[i][j])str = ":/res/Coin0001.png";
        // 加载金币
        MyCoin *coin = new MyCoin(str);
        coin->setParent(this);
        coin->move(59+i*50, 203+j*50);
        coin->posX = i;
        coin->posY=j;
        coin->flag = gameArray[i][j]; // 1金币 0 银币
        coinBtn[i][j]=coin;
        connect(coin, &QPushButton::clicked, [=](){
            if(!coin->isWin) flipSound->play();
            for(int i=0;i<4;++i)for(int j=0;j<4;++j)coinBtn[i][j]->isWin=true;
            coin->changeFlag();
            this->gameArray[i][j]^=1;

            // 延时改变周围
            QTimer::singleShot(300, [=](){
                if(coin->posX+1<4){
                    coinBtn[coin->posX+1][coin->posY]->changeFlag();
                    this->gameArray[coin->posX+1][coin->posY]^=1;
                }

                if(coin->posX>0){
                    coinBtn[coin->posX-1][coin->posY]->changeFlag();
                    this->gameArray[coin->posX-1][coin->posY]^=1;
                }


                if(coin->posY+1<4){
                    coinBtn[coin->posX][coin->posY+1]->changeFlag();
                    this->gameArray[coin->posX][coin->posY+1]^=1;
                }

                if(coin->posY>0){
                    coinBtn[coin->posX][coin->posY-1]->changeFlag();
                    this->gameArray[coin->posX][coin->posY-1]^=1;
                }
                // 判断是否胜利
                this->isWin=true;
                for(int i=0;i<4;++i)for(int j=0;j<4;++j)if(!coinBtn[i][j]->flag)this->isWin=false;
                if(this->isWin) {
                    winSound->play();
                    qDebug()<<"胜利了";
                    for(int i=0;i<4;++i)for(int j=0;j<4;++j)coinBtn[i][j]->isWin=true;
                    // 创建动画对象
                    QPropertyAnimation *animation = new QPropertyAnimation(winLabel, "geometry");

                    // 设置动画时间间隔
                    animation->setDuration(1000);

                    // 起始位置
                    animation->setStartValue(QRect(winLabel->x(), winLabel->y(), winLabel->width(), winLabel->height()));;
                    // 结束位置
                    animation->setEndValue(QRect(winLabel->x(), winLabel->y()+114, winLabel->width(), winLabel->height()));;
                    // 设置曲线
                    animation->setEasingCurve(QEasingCurve::OutBounce);
                    animation->start();
                }else {

                    for(int i=0;i<4;++i)for(int j=0;j<4;++j)coinBtn[i][j]->isWin=false;
                }


            });

        } );
    }

}

void PlayScene::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/res/PlayLevelSceneBg.png");
    painter.drawPixmap(0,0,width(), height(),pix);

    // 背景图标
    pix.load(":/res/Title.png");
    pix = pix.scaled(pix.width()/2, pix.height()/2);
    painter.drawPixmap(10,30,pix);
}
